package com.zcs.app.lucky.ui

import android.content.Intent
import android.text.TextUtils
import android.view.View
import cn.sxw.android.base.dialog.CustomDialogHelper
import cn.sxw.android.base.kt.BaseViewModel
import cn.sxw.android.base.utils.JLogUtil
import com.zcs.app.lucky.base.BaseLuckyActivity
import com.zcs.app.lucky.data.GroupDataCache
import com.zcs.app.lucky.data.LuckyDataCache
import com.zcs.app.lucky.data.PrizeDataCache
import com.zcs.app.lucky.databinding.ActivityCreateLuckyBinding
import com.zcs.app.lucky.entity.KEY_FOR_CHECK

class CreateLuckyActivity : BaseLuckyActivity<ActivityCreateLuckyBinding, BaseViewModel>() {
    // 懒加载机制
    private val mLucky by lazy {
        LuckyDataCache.loadAll()
        LuckyDataCache.findByIdOrNewInstance("")
    }

    // 程序已就绪
    override fun onReady() {
        binding.toolbar.tvToolbarTitle.text = "创建抽奖"
        binding.loadingLayout.visibility = View.VISIBLE
    }

    override fun onInit() {
        // 初始UI
        initView()
        // 初始化数据
        initData()
        // 初始化观察者
        initObserve()
    }

    override fun initObserve() {
        GroupDataCache.tempCheckedId.observe(this, {
            val currGroup = GroupDataCache.findByIdOnNull(it)
            if (currGroup != null) {
                mLucky.group = currGroup
                binding.btnAddGroup.visibility = View.GONE
                binding.tvGroupName.text = "${currGroup.niceName()}，点击切换"
                // 显示分组
                adapterHelper.userList.clear()
                adapterHelper.userList.addAll(currGroup.list)
                adapterHelper.initUserAdapter(binding.rvUsers, spanCount = 4) { id ->
                    JLogUtil.d("click id is $id")
                }
            }
        })
    }

    override fun initData() {
        adapterHelper.prizeList.clear()
        val list = PrizeDataCache.loadAllPrizes()
        if (list.isNotEmpty()) {
            adapterHelper.prizeList.addAll(list)
        }
        mLucky.prizeList = adapterHelper.prizeList
        adapterHelper.initPrizeAdapter(binding.rvPrize, horizontal = true) {
            onCreatePrize(it.id)
        }
        hideLoading()
        // 初始化抽奖列表
        LuckyDataCache.loadAll()
    }

    override fun initView() {
    }

    override fun hideLoading() {
        super.hideLoading()
        binding.loadingLayout.visibility = View.GONE
    }

    override fun addOnClickListener() {
        binding.toolbar.ivToolbarBack.setOnClickListener { onBackPressed() }
        binding.btnStart.setOnClickListener {
            onStartAfterSave()
        }
        // 添加奖品
        binding.btnAddPrize.setOnClickListener {
            onCreatePrize()
        }
        // 选择分组
        binding.tvGroupName.setOnClickListener {
            val intent = Intent(this, GroupListActivity::class.java)
            intent.putExtra(KEY_FOR_CHECK, true)
            launchActivity(intent)
        }
        binding.btnAddGroup.setOnClickListener {
            val intent = Intent(this, GroupListActivity::class.java)
            intent.putExtra(KEY_FOR_CHECK, true)
            launchActivity(intent)
        }
    }

    // 保存并开始
    private fun onStartAfterSave() {
        val name = binding.etName.text.toString().trim()
        if (name.isEmpty()) {
            showToast(binding.etName.hint.toString())
            return
        }
        val desc = binding.etDesc.text.toString().trim()
        if (desc.isEmpty()) {
            showToast(binding.etDesc.hint.toString())
            return
        }
        if (mLucky.prizeList.isNullOrEmpty()) {
            showToast("请添加奖品")
            return
        }
        if (mLucky.group == null) {
            showToast("请选择抽奖人员")
            return
        }
        mLucky.name = name
        mLucky.desc = desc
        mLucky.createTime = System.currentTimeMillis()
        LuckyDataCache.add(mLucky)
        killMyself()
        val intent = Intent(this, LuckingActivity::class.java)
        intent.putExtra(KEY_ID, mLucky.id)
        launchActivity(intent)
    }

    // 奖品列表
    private fun onCreatePrize(id: String = "") {
        goAdd = true
        if (TextUtils.isEmpty(id)) {
            launchActivity(AddPrizeActivity::class.java)
        } else {
            val intent = Intent(this, AddPrizeActivity::class.java)
            intent.putExtra(AddPrizeActivity.KEY_PRIZE_ID, id)
            launchActivity(intent)
        }
    }

    override fun onBackPressed() {
        CustomDialogHelper.showCustomConfirmDialog(
            this,
            "正在创建抽奖活动，你确定要退出吗？",
            object : CustomDialogHelper.NativeDialogCallback {
                override fun onConfirm() {
                    killMyself()
                }

                override fun onCancel() {}
            })
    }
}